﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ObTookit.Login;
using ObTookit.Model;

namespace ObTookit
{
    public partial class FormLogin : Form
    {
        private string name = "";
        private string pwd = "";

        public FormLogin()
        {
            InitializeComponent();
        }

        private void FormLogin_Load(object sender, EventArgs e)
        {
            this.tbx_login_name.Text = "obsidian";
            this.tbx_login_pwd.Text = "ob";
        }

        private void btn_login_Click(object sender, EventArgs e)
        {
            name = this.tbx_login_name.Text.Trim();
            pwd = this.tbx_login_pwd.Text;
            if (string.IsNullOrWhiteSpace(name))
            {
                MessageBox.Show("请输入用户名！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if(string.IsNullOrWhiteSpace(pwd))
            {
                MessageBox.Show("请输入密码！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (VerifyUser())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private bool VerifyUser()
        {
            bool verify = false;
            UserEO user = new UserBL().GetUser(name);
            if (user != null)
            {
                if(pwd.Equals(user.Password))
                {
                    verify = true;
                }
                else
                {
                    MessageBox.Show("用户[" + name + "]密码错误！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    tbx_login_pwd.Clear();
                    tbx_login_pwd.Focus();
                }
            }
            else
            {
                MessageBox.Show("用户[" + name + "]不存在！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbx_login_name.Clear();
                tbx_login_pwd.Clear();
                tbx_login_name.Focus();
            }
            return verify;
        }

        private void pbx_login_exit_Click(object sender, EventArgs e)
        {
            this.Dispose();
            Application.Exit();
        }
    }
}
